<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-23 21:09:36
 * @LastEditTime: 2023-12-02 09:34:13
 * @LastEditors: cuicui
-->
<template>
  <business-layout title='日志列表' custom-title>
    <div class='table-page-search-wrapper' slot='header'>
      <a-row style='height: 40px;'>
        <a-form style='height: 40px;' :form='form1' layout='inline' @submit='handleQueryData'>
          <a-col :span='4' style='height: 40px;'>
            <a-form-item>
              <a-input v-model='queryParam.operator' placeholder='请输入用户名' />
            </a-form-item>
          </a-col>
          <a-col :span='12' style='height: 40px;margin-left: 10px'>
            <a-form-item>
              <a-date-picker
                v-model='queryParam.startTime'
                valueFormat='YYYY-MM-DD 00:00:00'
                format='YYYY-MM-DD 00:00:00'
                placeholder='请选择开始时间'
                :disabled-date='startDisabledDate'
              />
              <a-date-picker
                v-model='queryParam.endTime'
                style='margin-left: 10px'
                :disabled-date='endDisabledDate'
                format='YYYY-MM-DD 23:59:59'
                valueFormat='YYYY-MM-DD 23:59:59'
                placeholder='请选择结束时间'
              />
              <a-button type='primary' html-type='submit' style='margin-left: 10px;' >
                <a-icon type='search' />
                查询
              </a-button>
              <a-button html-type='reset' style='margin-left: 10px;' @click='handleResetDate'>
                重置
              </a-button>
            </a-form-item>
          </a-col>
        </a-form>
      </a-row>
    </div>
    <s-table
      ref='table'
      size='default'
      rowKey='id'
      :columns='columns'
      :data='loadData'
      showPagination='auto'
    >
      <span slot='serial' slot-scope='text, record, index'>
        {{ index + 1 }}
      </span>
      <span slot='status' slot-scope='text'>
        <a-badge :status='text | statusTypeFilter' :text='text | statusFilter' />
      </span>
      <span slot='description' slot-scope='text'>
        <ellipsis :length='4' tooltip>{{ text }}</ellipsis>
      </span>
      <span slot='createdTime' slot-scope='text, record, index'>
        {{ text | dayjs }}
      </span>

      <span slot='action' slot-scope='text, record'>
        <template>
          <a class='del-link' @click='showDeleteConfirm(record)'>删除</a>
        </template>
      </span>
    </s-table>

    <!--    <create-form-->
    <!--      ref='createModal'-->
    <!--      :visible='visible'-->
    <!--      :loading='confirmLoading'-->
    <!--      :model='mdl'-->
    <!--      @cancel='handleCancel'-->
    <!--      @ok='handleOk'-->
    <!--    />-->
    <!--    <assign-->
    <!--      ref='createAssign'-->
    <!--      :visible='assignVisible'-->
    <!--      :loading='assignLoading'-->
    <!--      @cancel='handleAssignCancel'-->
    <!--      @ok='handleAssignOk'-->
    <!--    />-->
  </business-layout>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getRoleList, getServiceList } from '@/api/manage'
import { findLog } from '@/api/log'
import CreateForm from '../role/CreateForm'
import Assign from '../role/assign'
import moment from 'moment'

const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '操作人',
    dataIndex: 'createdUser',
    scopedSlots: { customRender: 'createdUser' }
  },
  {
    title: '操作时间',
    dataIndex: 'createdTime',

    scopedSlots: { customRender: 'createdTime' }
  },
  {
    title: '操作内容',
    dataIndex: 'operationContent'
  },
  {
    title: '操作设备',
    dataIndex: 'userAgent'
  },
  {
    title: '操作设备IP',
    dataIndex: 'operatorIpaddr'
  }
]

const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}

export default {
  name: 'Log',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    Assign
  },
  data() {
    this.columns = columns
    return {
      dateFormat: 'YYYY/MM/DD',
      form1: this.$form.createForm(this),
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      assignLoading: false,
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return findLog(requestParameters)
          .then(res => {
            return res
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  created() {

  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  mounted() {
  },
  beforeDestroy() { // 订阅事件记得要取消
  },
  methods: {
    startDisabledDate(startValue) {
      const endValue = this.queryParam.endTime
      if (!startValue || !endValue) {
        return false
      }
      return startValue.valueOf() > moment(endValue).valueOf()
    },
    endDisabledDate(endValue) {
      const startValue = this.queryParam.startTime
      if (!startValue || !endValue) {
        return false
      }
      return moment(startValue).valueOf() >= endValue.valueOf()
    },
    getCurrentStyle(current, today) {
      const style = {}
      if (current.date() === 1) {
        style.border = '1px solid #1890ff'
        style.borderRadius = '50%'
      }
      return style
    },
    handleQueryData(e) {
      e.preventDefault()
      const form = this.form1
      const that = this
      const queryObj = {}
      form.validateFields((error, values) => {
        Object.keys(values).forEach(key => {
          if (values[key]) {
            queryObj[key] = values[key]
          }
        })
        // this.queryParam = queryObj
        that.$refs.table.refresh()
      })
    },
    handleResetDate() {
      const form = this.form1
      form.resetFields()
      this.queryParam = {}
      this.handleQueryData()
    },
    handleAssignOk() {
      this.assignVisible = false
    },
    handleAssignCancel() {
      this.assignVisible = false
    },
    testSocketIo() {
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('更新成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    showDeleteConfirm(record) {
      this.$confirm({
        title: '提示?',
        content: '当前操作不可逆、是否继续？',
        okType: 'danger',
        onOk() {
          this.$message.success(`${record.no} 删除成功`)
        },
        onCancel() {
          console.log('Cancel')
        }
      })
    },
    handleSub(record) {
      this.$confirm({
        title: '提示?',
        content: '确定要锁定所选用户么？',
        onOk() {
          this.$message.success(`${record.no} 锁定成功`)
        },
        onCancel() {
          console.log('Cancel')
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
